2 Problem: 11116 - Babel towers
3 (From the UVa Online Judge)
5 Author: Andrés Mejía-Posada
17 const double EPS
= 10E-9;
22 disc(double X
, double Y
, double R
) : x(X
), y(Y
), r(R
){}
25 //Represents a mass "m" centered at point ("x", "y")
29 center(double X
, double Y
, double M
) : x(X
), y(Y
), m(M
){}
30 center
&operator = (const center
&b
){
39 //Returns the new mass formed by combining both masses "a" and "b" into a puntual mass.
40 center
operator | (const center
&a
, const center
&b
){
42 r
.x
= (a
.x
* a
.m
+ b
.x
* b
.m
) / (a
.m
+ b
.m
);
43 r
.y
= (a
.y
* a
.m
+ b
.y
* b
.m
) / (a
.m
+ b
.m
);
49 ostream
& operator<<( ostream
& out
, const center
&c
) {
50 out
<< setprecision(2) << "(" << c
.x
<< ", " << c
.y
<< ", " << c
.m
<< ")";
55 ostream
& operator<<( ostream
& out
, const disc
&c
) {
56 out
<< setprecision(2) << "(" << c
.x
<< ", " << c
.y
<< ", " << c
.r
<< ")";
61 //True if the center of mass "C" is outside of the base of disc "D"
62 inline bool outside(const center
&c
, const disc
&d
){
63 return fabs((c
.x
-d
.x
)*(c
.x
-d
.x
) + (c
.y
-d
.y
)*(c
.y
-d
.y
)) > d
.r
*d
.r
- EPS
;
68 while (cin
>> n
&& n
){
71 for (int i
=0; i
<n
; ++i
){
80 for (int j
=1; j
< n
&& ok
; ++j
){
81 center
c(d
[j
].x
, d
[j
].y
, d
[j
].r
* d
[j
].r
);
83 for (int i
=j
-1; i
>= 0 && ok
; --i
){
84 if (outside(c
, d
[i
])){
88 c
= c
| center(d
[i
].x
, d
[i
].y
, d
[i
].r
*d
[i
].r
);
92 cout
<< (ok
?"F":"Unf") << "easible";
93 if (!ok
) cout
<< " " << k
;